<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>购物车页面</title>
</head>
<body>
	<%@ include file="/WEB-INF/jsp/top.jsp" %>
	<h6><a href="${pageContext.request.contextPath }/index2">回到主页</a></h6>
	<div>
		<button type="button" onclick="deleteBatch()">批量删除</button>
	</div>
	<div><table border="2" id="tab1">
		<tr><th><input type='checkbox' id='allCheckbox' onclick='allCheck()'>全选
			</th><th>商品图片</th><th>商品名字</th><th>商品描述</th><th>商品单价</th><th>商品数量</th><th>商品金额</th><th>操作</th></tr>
	</table></div>
	<div id="div2"></div>
	<div id="div1"></div>
</body>

<script type="text/javascript" src="${pageContext.request.contextPath }/statics/js/jquery-3.5.1.js"></script>
<script type="text/javascript">
	$(function(){
		start(1);
	});
	var jsonObj={"pageNum":1};
	function start(start){
		jsonObj.pageNum=start;
		$.ajax({
			type:"post",
			data:jsonObj,
			url:"${pageContext.request.contextPath}/car/selectAllCarItemByUid",
			success:function(result){
				var object="<tr><th><input type='checkbox' id='allCheckbox' onclick='allCheck()'>全选"
					+"</th><th>商品图片</th><th>商品名字</th><th>商品描述</th><th>商品单价</th><th>商品数量</th><th>商品金额</th><th>操作</th></tr>";
				$.each(result.list,function(i,n){
					object+="<tr><td><input type='checkbox' id='"+(n.store.sid)*1000+"' name='"+n.count+"' nonce='"+n.subtotal+"' onclick='js("
						+n.store.sid+","+n.count+","+n.subtotal+")'></td>"//c:if不起作用了，以后用数据库的
						+"<td>"+n.store.cid+"</td>"
						+"<c:if test='n.store.cid==2'><td><img alt='沙发' src='${pageContext.request.contextPath }/statics/img/shafa.JPG' height='100' width='75'></td></c:if><td>"
						+n.store.storeName+"</td><td>要个鸡儿描述</td><td>"+n.store.price
						+"</td><td><button onclick='sub("+n.store.sid+","+n.count+","+n.store.price+")'>-</button>"
						+"<input id='"+i+"' type='number' name='"+n.store.sid+"n' value='"+n.count+"' onblur='shiqu("+i+","+n.store.sid+","+n.count+","+n.store.price+")'/>"
						+"<button onclick='add("+n.store.sid+","+n.count+","+n.store.price+")'>+</button></td><td>"+n.subtotal
						+"</td><td><a href='#' onclick='deleteOne("+n.store.sid+")'>删除</td><tr>";
				});	
				object+="<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>"
					+"<td>已选商品</td><td><input id='cou' type='text' name='cou' value='0'></input></td><td>件,合计（不含运费）：</td>"
					+"<td><input id='js' type='text' name='subto' value='0'></input></td><td><a href='#' onclick='jiesuan()'>结算</a></td></tr>";
				$("#tab1").html(object);
				
				var object1="当前第"+result.pageNum+"页，总共"+result.pages+"页，总共"+result.total+"条记录";
				$("#div2").html(object1);
				//为了删除数据回到原来页面，而不是回到首页
				$("#div2").prop("is",result.pageNum);
				$("#div2").prop("nonce",result.size);
				
				
				var object2="";
				if(result.isFirstPage){
					object2+="<a>首页</a>";
				}else{
					object2+="<a href='#' onclick='pageChange(1)'>首页</a>";
				}
				if(result.hasPreviousPage){
					object2+="<a href='#' onclick='pageChange("+(result.pageNum-1)+")'>&laquo;</a>";
				}
				$.each(result.navigatepageNums,function(i,n){
					if(n==result.pageNum){
						object2+="<a>"+n+"</a>";
					}else{
						object2+="<a href='#' onclick='pageChange("+n+")'>"+n+"</a>";
					}
				});	
				if(result.hasNextPage){
					object2+="<a href='#' onclick='pageChange("+(result.pageNum+1)+")'>&raquo;</a>";
				}
				if(result.isLastPage){
					object2+="<a>末页</a>";
				}else{
					object2+="<a href='#' onclick='pageChange("+result.pages+")'>末页</a>";
				}
				$("#div1").html(object2);
			}
		});
	}
	
	function pageChange(currentPage){
		start(currentPage);
	}

	function allCheck(){//注意！！！动态的不能用click事件，还有用到了dom的方法需要把jQuery对象转换成dom对象
		$("table tr td input[type='checkbox']").prop("checked",$("#allCheckbox")[0].checked);

		var all=$("table tr td input:checked");
		if(all.length==0){
			document.getElementById("cou").value=0;
			document.getElementById("js").value=0;
			return false;
		}
		$.each(all,function(i,n){
			document.getElementById("cou").value=Number(document.getElementById("cou").value)+Number(n.name);
			document.getElementById("js").value=Number(document.getElementById("js").value)+Number(n.nonce);
		});
	}

	function deleteOne(suibian){
		$.ajax({
			type:"post",
			url:"${pageContext.request.contextPath}/car/deleteBatch",
			data:{
				["allList[0].id"]:suibian
			},
			success:function(result){
				if(result>0){
					if($("#div2")[0].nonce==result){//当前页数量等于删除的记录数
						start($("#div2")[0].is-1);
					}else{
						start($("#div2")[0].is);
					}
					window.location.href="${pageContext.request.contextPath}/car/car";
				}
				if(result==-1){
					alert("用户已取消删除");
				}
			}
		});
	}

	function deleteBatch(){
		var all=$("table tr td input:checked");
		if(all.length==0){
			alert("没有要删除的对象，请重新选择");
			return false;
		}
		var jsonObj2={};
		$.each(all,function(i,n){
			jsonObj2["allList["+i+"].id"]=(n.id)/1000;
		});
		$.ajax({
			type:"post",
			url:"${pageContext.request.contextPath}/car/deleteBatch",
			data:jsonObj2,
			success:function(result){
				if(result>0){
					alert("删除"+result+"条记录成功");
					if($("#div2")[0].nonce==result){//当前页数量等于删除的记录数
						if($("#div2")[0].is==1){
							start(1);
						}else{
							start($("#div2")[0].is-1);
						}
					}else{
						start($("#div2")[0].is);
					}
					window.location.href="${pageContext.request.contextPath}/car/car";
				}
				if(result==-1){
					alert("用户已取消删除");
				}
			}
		});
	}
	
	function deleteBatch1(){
		var all=$("table tr td input:checked");
		if(all.length==0){
			alert("没有要删除的对象，请重新选择");
			return false;
		}
		var jsonObj2={};
		$.each(all,function(i,n){
			jsonObj2["allList["+i+"].id"]=(n.id)/1000;
		});
		$.ajax({
			type:"post",
			url:"${pageContext.request.contextPath}/car/deleteBatch1",
			data:jsonObj2,
			success:function(result){
				if(result>0){
					alert("删除并添加进订单"+result+"条记录成功");
					if($("#div2")[0].nonce==result){//当前页数量等于删除的记录数
						if($("#div2")[0].is==1){
							start(1);
						}else{
							start($("#div2")[0].is-1);
						}
					}else{
						start($("#div2")[0].is);
					}
					window.location.href="${pageContext.request.contextPath}/orders/orderss";
				}
				if(result==-1){
					alert("用户已取消删除");
				}
			}
		});
	}

	$("#logOut").click(function(){
		window.location.href="${pageContext.request.contextPath}/logout";
	});

	function sub(n1,n2,n3){
		alert("sub");
		if(n2==1){
			return false;
		}
		$.ajax({
			type:"post",
			url:"${pageContext.request.contextPath}/car/updateCarItem?key=sub",
			/* data:{"sid":""+n.id,"count":""+n.count,"price":""+n.store.price}, */
			data:{"sid":n1,"count":n2,"price":n3},
			/* data:{}, */
			success:function(result){
				if(result==0){
					alert("-1");
					start(1);
				}else{
					alert("已经最小");
				}
			}
		});
	}
	function add(n1,n2,n3){
		alert("add");
		var jsonAdd={};
		if(n2==99){
			return false;
		}
		$.ajax({
			type:"post",
			url:"${pageContext.request.contextPath}/car/updateCarItem?key=add",
			/* data:{"sid":""+n.id,"count":""+n.count,"price":""+n.store.price}, */
			data:{"sid":n1,"count":n2,"price":n3},
			/* data:{}, */
			success:function(result){
				alert("+1");
				start(1);
			}
		});
	}
	function shiqu(n,n1,n2,n3){
		alert(n+","+n1+","+n2+","+n3);//搞了半天呀，用n最后值为on，用n1就好了！！！
		var i=parseInt(document.getElementById(n).value);//找到原因了，是因为id属性值重复了，所以模棱两可的值为on
		alert(i);
		if(i==n2){
			document.getElementById(n).value=i;
			return false;
		}
		$.ajax({
			type:"post",
			url:"${pageContext.request.contextPath}/car/updateCarItem?key=shiqu",
			data:{"sid":n1,"count":i,"price":n3},
			success:function(result){
				alert(result);
				start(1);
			}
		});
	} 
	function js(s1,s2,s3){
		var cou=Number(document.getElementById("cou").value);
		var js=Number(document.getElementById("js").value);
		if(document.getElementById(s1*1000).checked){
			//$("#js").val()=s2;
			document.getElementById("cou").value=cou+Number(s2);
			document.getElementById("js").value=js+Number(s3);
			//document.getElementById("js").innerHTML=s3;
		}else{
			document.getElementById("cou").value=cou-Number(s2);
			document.getElementById("js").value=js-Number(s3);
		}
	}
	function jiesuan(){
		var all=$("table tr td input:checked");
		if(all.length==0){
			alert("没有要结算的对象，请重新选择");
			return false;
		}
		var jsonObj2={};
		$.each(all,function(i,n){
			jsonObj2["carItemList["+i+"].store.sid"]=(n.id)/1000;
			jsonObj2["carItemList["+i+"].count"]=n.name;
			jsonObj2["carItemList["+i+"].subtotal"]=n.nonce;
		});
		$.ajax({
			type:"post",
			url:"${pageContext.request.contextPath}/orders/insertOrders",
			data:jsonObj2,
			success:function(result){
				alert(result);
				deleteBatch1();
				alert("start");
				window.location.href="${pageContext.request.contextPath}/orders/fukuan?oid="+result.oid+"&total="+result.total;
				alert("over");
			}
		});
	}
</script>

</html>